<?php

use yii\db\Migration;

/**
 * Handles the creation of table `article_image`.
 */
class m190127_063220_create_article_image_table extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $this->createTable('article_image', [
            'id'       => $this->primaryKey(),
            'aid'      => $this->integer(10)->unsigned(),
            'image_id' => $this->integer(),
        ]);
        $this->addColumn('article', 'user_id', $this->integer());
        $this->addForeignKey('fk_article_user_id', 'article', 'user_id', 'user', 'id');
        $this->addForeignKey('fk_article_image_image_id', 'article_image', 'image_id', 'image', 'id');
        $this->addForeignKey('fk_article_image_article_id', 'article_image', 'aid', 'article', 'id');

    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->dropForeignKey('fk_article_user_id');
        $this->dropForeignKey('fk_article_image_image_id');
        $this->dropForeignKey('fk_article_image_article_id');
        $this->dropTable('article', 'user_id');
        $this->dropTable('article_image');
    }
}
